/**
 * author:Administrator
 * date:2015/7/27
 * desc:TODO
 */

'use strict';
let mongoDao = new require('./mongo/Permission.js')();
let fout = require('../common/function.js').formatOutput;

function DaoDispatcher() {
    this.pCache = null; //permission cache
//cache的结构
//    {
//        "商家管理": [ "添加商家", "编辑商家" ],
//        "商家详情": [ "商家详情"]
//    }
    this.updatedAt = null;
}
module.exports = new DaoDispatcher();

let theProto = DaoDispatcher.prototype;

theProto.getUniqueName = function (gName, name) {
    return mongoDao.getUniqueName(gName, name);
};

theProto.parseUniqueName = function (uniqueName) {
    return mongoDao.parseUniqueName(uniqueName);
};

// 检测某项权限是否存在
theProto.exists = function* (gName, name) {
    if (!this.pCache) {
        yield this.refreshCache();
    }
    if (!this.pCache[gName]) {
        return {error: '不存在的权限组:' + gName};
    }
    if (this.pCache[gName].indexOf(name) < 0) {
        return {error: '权限组[' + gName + ']无权限项:' + name};
    }
    return {
        error: null
    };
};

/*
 {
 "商家管理": ["添加商家","编辑商家"],
 "商家详情": ["商家详情"]
 }
 */
theProto.addBatch = function*(inParams) {
    let list = [];
    let self = this;
    for (let gName in inParams) {
        let ps = inParams[gName];
        ps.forEach(function (item) {
            list.push({
                groupName: gName,
                name: item,
                uniqueName: self.getUniqueName(gName, item)
            });
        })
    }
    let result = yield mongoDao.saveAll(list);
    yield this.refreshCache();
    return result;
};

/**
 * 获取整个权限图
 * @returns
 *
 {
 "商家管理": [ "添加商家", "编辑商家" ],
 "商家详情": [ "商家详情", "编辑店面", "添加特惠商品", "编辑特惠商品", "评价管理" ]
 }
 */
theProto.getAll = function*() {
    if (!this.pCache) {
        yield this.refreshCache();
    }

    // need return copy, not self
    var p = this.pCache;
    var cp = JSON.parse(JSON.stringify(p));
    return fout(0, null, cp);
};

/**
 * refresh cache, cache的结构
 {
 "商家管理": [ "添加商家", "编辑商家" ],
 "商家详情": [ "商家详情", "编辑店面", "添加特惠商品", "编辑特惠商品", "评价管理" ]
 }
 */
theProto.refreshCache = function*() {
    this.pCache = null;
    let result = yield mongoDao.getAll();
    // TODO need build structure

    var list = {};
    while (result.length > 0) {
        var item = result.shift();
        let gName = item.groupName;
        if (!list[gName]) list[gName] = [];
        list[gName].push(item.name);
    }

    this.pCache = list;
    this.updatedAt = new Date().getTime();//
};

/**
 * 检测权限是否存在
 {
     "商家管理": ["添加商家","编辑商家"],
     "商家详情": ["商家详情"]
 }
 *
 * }]
 */
theProto.checkPermissionExists = function*(inParams) {
    if (!this.pCache) {
        yield this.refreshCache();
    }

    for (let gName in inParams) {
        let cachedGroup = this.pCache[gName];
        if (!cachedGroup) {
            return {error: "不存在的权限组:" + gName}
        }

        for (let i = 0; i < inParams[gName].length; i++) {
            let name = inParams[gName][i];
            if (cachedGroup.indexOf(name) < 0) {
                return {error: "权限组[" + gName + "]不存在权限:" + name}
            }
        }
    }

    return {error: null}
};

